﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.ComponentModel;

namespace yinyue
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private Songlist[] song = null;
        private void button1_Click(object sender, EventArgs e)
        {
            song = HttpTool.getInstance().getHttp("5");
            dataGridView1.DataSource = song;
            contextMenuStrip1.Items[0].Visible = true;
            contextMenuStrip1.Items[1].Visible = false;
            
        }
        HuanCun huan = new HuanCun();
        int a = 0;//0是所有1是我喜欢2是历史播放
        private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            i = 0;
            shuzu = 0;
            Songlist song = selectXinXi();
            bofang(song);
            HistoricalPlay.getHistoricalPlay().addSong(song);
            pictureBox1.Image =  Image.FromFile(TuPian.getAddTuPian().getAlbumpic_small(song.songid.ToString()));
            label1.Text = TuPian.getAddTuPian().getSingerName(song.songid.ToString());
            label2.Text = TuPian.getAddTuPian().getSongNameg(song.songid.ToString());
        }

        private void bofang(Songlist song)
        {
            string tupian = "D:\\音乐\\" + song.songid + "\\" + song.songid + ".jpg";
            string path = "D:\\音乐\\" + song.songid + "\\" + song.songid + ".mp3";
            if (huan.Orexist(path))
            {
                axWindowsMediaPlayer1.URL = path;
            }
            else
            {
                axWindowsMediaPlayer1.URL = song.url;
                huan.HttpDownloadFile(song.url, path, song.songid.ToString());
                huan.HttpDownloadFile(song.albumpic_small, tupian, song.songid.ToString());
                huan.XinXi(song, "D:\\音乐\\" + song.songid + "\\" + song.songid + ".txt");
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (axWindowsMediaPlayer1.settings.mute)
            {
                axWindowsMediaPlayer1.settings.mute = false;
                toolStripButton1.Text = "静音 ";
                return;
            }
                axWindowsMediaPlayer1.settings.mute = true;
                toolStripButton1.Text = "静音 ";
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            if (axWindowsMediaPlayer1.status.Equals("已暂停")) 
            {
                axWindowsMediaPlayer1.Ctlcontrols.play();
                toolStripButton2.Text = "暂停";
                return;
            }
                axWindowsMediaPlayer1.Ctlcontrols.pause();
                toolStripButton2.Text = "播放";
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            axWindowsMediaPlayer1.Ctlcontrols.stop();
            axWindowsMediaPlayer1.Ctlcontrols.play();
        }
        int shuzu = 0;
        int i = 0;
        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            List<Songlist> songlist = HistoricalPlay.getHistoricalPlay().getList();
            if (i==0)
            {
                foreach (var item in songlist)
                {
                    ++shuzu;
                    if (item.songid.ToString() == dataGridView1.SelectedRows[0].Cells["songid"].Value.ToString())
                    {
                        if (shuzu == songlist.Count)
                        {
                            shuzu = 0;
                            bofang(songlist[shuzu]);
                        }
                        else
                        {
                            bofang(songlist[shuzu]);
                        }
                        pictureBox1.Image = Image.FromFile(TuPian.getAddTuPian().getAlbumpic_small(songlist[shuzu].songid.ToString()));
                        label1.Text = TuPian.getAddTuPian().getSingerName(songlist[shuzu].songid.ToString());
                        label2.Text = TuPian.getAddTuPian().getSongNameg(songlist[shuzu].songid.ToString());
                        i = 1;
                        break;
                    }
                    
                }
            }
            else
            {
                shuzu++;
                if (shuzu == songlist.Count)
                {
                    shuzu = 0;
                    bofang(songlist[shuzu]);
                }
                else 
                {
                    string name = songlist[shuzu].songname;
                    bofang(songlist[shuzu]);
                }
               
                pictureBox1.Image = Image.FromFile(TuPian.getAddTuPian().getAlbumpic_small(songlist[shuzu].songid.ToString()));
                label1.Text = TuPian.getAddTuPian().getSingerName(songlist[shuzu].songid.ToString());
                label2.Text = TuPian.getAddTuPian().getSongNameg(songlist[shuzu].songid.ToString());
            }
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            List<Songlist> list = new List<Songlist>();
            List<Songlist> like = new List<Songlist>();
            if (textBox1.Text=="")
            {
                MessageBox.Show("请输入歌名或者歌手！");
            }
            if (delet == 1)
            {
                like = HistoricalPlay.getHistoricalPlay().getList();
            } if (delet==0)
            {
                like = Like.getLike().getSongList();
            }
            else
            {
                for (int i = 0; i < song.Length; i++)
                {
                    if (song[i].singername.IndexOf(textBox1.Text) != -1 || song[i].songname.IndexOf(textBox1.Text) != -1)
                    {
                        list.Add(
                            new Songlist()
                            {
                                albumid = song[i].albumid,
                                albummid = song[i].albummid,
                                seconds = song[i].seconds,
                                albumpic_big = song[i].albumpic_big,
                                albumpic_small = song[i].albumpic_small,
                                downUrl = song[i].downUrl,
                                singerid = song[i].singerid,
                                singername = song[i].singername,
                                songid = song[i].songid,
                                songname = song[i].songname,
                                url = song[i].url
                            }
                           );
                    }
                }
                dataGridView1.DataSource = list;
                textBox1.Text = "";
                return;
            }
            foreach (var item in like)
            {
                if (item.singername.IndexOf(textBox1.Text) != -1 || item.songname.IndexOf(textBox1.Text) != -1)
                {
                    list.Add(
                        new Songlist()
                        {
                            albumid = item.albumid,
                            albummid = item.albummid,
                            seconds = item.seconds,
                            albumpic_big = item.albumpic_big,
                            albumpic_small = item.albumpic_small,
                            downUrl = item.downUrl,
                            singerid = item.singerid,
                            singername = item.singername,
                            songid = item.songid,
                            songname = item.songname,
                            url = item.url
                        });
                }
            }
            dataGridView1.DataSource = list;
            textBox1.Text = "";
            
        }

        private void 添加此歌曲ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Songlist song = selectXinXi();
            bool bo = Like.getLike().addSong(song);
            if (bo == true)
            {
                MessageBox.Show("添加成功！");
            }
            else 
            {
                MessageBox.Show("添加失败！");
            }
        }

        private Songlist selectXinXi()
        {
            string songid = dataGridView1.SelectedRows[0].Cells["songid"].Value.ToString();
            string url = dataGridView1.SelectedRows[0].Cells["url"].Value.ToString();
            string songname = dataGridView1.SelectedRows[0].Cells["songname"].Value.ToString();
            string seconds = dataGridView1.SelectedRows[0].Cells["seconds"].Value.ToString();
            string albummid = dataGridView1.SelectedRows[0].Cells["albummid"].Value.ToString();
            string albumpic_small = dataGridView1.SelectedRows[0].Cells["albumpic_small"].Value.ToString();
            string singerid = dataGridView1.SelectedRows[0].Cells["singerid"].Value.ToString();
            string albumpic_big = dataGridView1.SelectedRows[0].Cells["albumpic_big"].Value.ToString();
            string downUrl = dataGridView1.SelectedRows[0].Cells["downUrl"].Value.ToString();
            string singername = dataGridView1.SelectedRows[0].Cells["singername"].Value.ToString();
            string albumid = dataGridView1.SelectedRows[0].Cells["albumid"].Value.ToString();
            Songlist song = new Songlist();
            song.albumid = Convert.ToInt32(albumid);
            song.albummid = albummid;
            song.albumpic_big = albumpic_big;
            song.albumpic_small = albumpic_small;
            song.downUrl = downUrl;
            song.seconds = Convert.ToInt32(seconds);
            song.singerid = Convert.ToInt32(singerid);
            song.singername = singername;
            song.songid = Convert.ToInt32(songid);
            song.songname = songname;
            song.url = url;
            return song;
        }
        private Songlist selectXinXi2()
        {
            string songid = dataGridView2.SelectedRows[0].Cells["songid2"].Value.ToString();
            string url = dataGridView2.SelectedRows[0].Cells["url2"].Value.ToString();
            string songname = dataGridView2.SelectedRows[0].Cells["songname2"].Value.ToString();
            string seconds = dataGridView2.SelectedRows[0].Cells["seconds2"].Value.ToString();
            string albummid = dataGridView2.SelectedRows[0].Cells["albummid2"].Value.ToString();
            string albumpic_small = dataGridView2.SelectedRows[0].Cells["albumpic_small2"].Value.ToString();
            string singerid = dataGridView2.SelectedRows[0].Cells["singerid2"].Value.ToString();
            string albumpic_big = dataGridView2.SelectedRows[0].Cells["albumpic_big2"].Value.ToString();
            string downUrl = dataGridView2.SelectedRows[0].Cells["downUrl2"].Value.ToString();
            string singername = dataGridView2.SelectedRows[0].Cells["singername2"].Value.ToString();
            string albumid = dataGridView2.SelectedRows[0].Cells["albumid2"].Value.ToString();
            Songlist song = new Songlist();
            song.albumid = Convert.ToInt32(albumid);
            song.albummid = albummid;
            song.albumpic_big = albumpic_big;
            song.albumpic_small = albumpic_small;
            song.downUrl = downUrl;
            song.seconds = Convert.ToInt32(seconds);
            song.singerid = Convert.ToInt32(singerid);
            song.singername = singername;
            song.songid = Convert.ToInt32(songid);
            song.songname = songname;
            song.url = url;
            return song;
        }
        
        private void button3_Click(object sender, EventArgs e)
        {
            delet = 0;
            contextMenuStrip1.Items[0].Visible = false;
            contextMenuStrip1.Items[1].Visible = true;
            dataGridView1.DataSource=Like.getLike().getSongList();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            delet = 1;
            contextMenuStrip1.Items[0].Visible = true;
            contextMenuStrip1.Items[1].Visible = true;
            dataGridView1.DataSource = HistoricalPlay.getHistoricalPlay().getList();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            BindingSource bs2 = new BindingSource();
            bs2.DataSource = HistoricalPlay.getHistoricalPlay().getList();
            dataGridView2.DataSource = bs2;
            if (dataGridView2.Visible == true)
            {
                dataGridView2.Visible = false;
            }
            else
            {
                dataGridView2.Visible = true;
            }
        }

        private void dataGridView2_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            Songlist song = selectXinXi2();
            bofang(song);
            HistoricalPlay.getHistoricalPlay().addSong(song);
            pictureBox1.Image = Image.FromFile(TuPian.getAddTuPian().getAlbumpic_small(song.songid.ToString()));
            label1.Text = TuPian.getAddTuPian().getSingerName(song.songid.ToString());
            label2.Text = TuPian.getAddTuPian().getSongNameg(song.songid.ToString());
        }
        int delet = 0;
        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Songlist song = selectXinXi();
            if (delet==0)
            {
                BindingSource bs2 = new BindingSource();
                bs2.DataSource = Like.getLike().deleteList(song.songid);
                dataGridView1.DataSource = bs2;
            }
            else
            {
                BindingSource bs3 = new BindingSource();
                bs3.DataSource =  HistoricalPlay.getHistoricalPlay().deleteList(song.songid);
                dataGridView1.DataSource = bs3;
            }
            
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            string s = axWindowsMediaPlayer1.status;
            if (s.Equals("已停止"))
            {
                 List<Songlist> songlist = HistoricalPlay.getHistoricalPlay().getList();
            if (i==0)
            {
                foreach (var item in songlist)
                {
                    ++shuzu;
                    if (item.songid.ToString() == dataGridView1.SelectedRows[0].Cells["songid"].Value.ToString())
                    {
                        if (shuzu == songlist.Count)
                        {
                            shuzu = 0;
                            bofang(songlist[shuzu]);
                        }
                        else
                        {
                            bofang(songlist[shuzu]);
                        }
                        pictureBox1.Image = Image.FromFile(TuPian.getAddTuPian().getAlbumpic_small(songlist[shuzu].songid.ToString()));
                        label1.Text = TuPian.getAddTuPian().getSingerName(songlist[shuzu].songid.ToString());
                        label2.Text = TuPian.getAddTuPian().getSongNameg(songlist[shuzu].songid.ToString());
                        i = 1;
                        break;
                    }
                    
                }
            }
            else
            {
                shuzu++;
                if (shuzu == songlist.Count)
                {
                    shuzu = 0;
                    bofang(songlist[shuzu]);
                }
                else 
                {
                    bofang(songlist[shuzu]);
                }
               
                pictureBox1.Image = Image.FromFile(TuPian.getAddTuPian().getAlbumpic_small(songlist[shuzu].songid.ToString()));
                label1.Text = TuPian.getAddTuPian().getSingerName(songlist[shuzu].songid.ToString());
                label2.Text = TuPian.getAddTuPian().getSongNameg(songlist[shuzu].songid.ToString());
            }
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            song = HttpTool.getInstance().getHttp("4");
            dataGridView1.DataSource = song;
            contextMenuStrip1.Items[0].Visible = true;
            contextMenuStrip1.Items[1].Visible = false;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            song = HttpTool.getInstance().getHttp("3");
            dataGridView1.DataSource = song;
            contextMenuStrip1.Items[0].Visible = true;
            contextMenuStrip1.Items[1].Visible = false;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView2.Visible = false;
        }



    }
}
